const Com = require("./virus");
cc.Class({
    extends: Com,

    properties: {
    },

    ctor(){
        this.setAttr("updateOnOff" , false );
        this._vinf = {
            // sr : .5,
            // //背景
            // "node" : {
            //     w : 320 ,
            //     h : 320 ,
            //     sr : 1
            // },
            // "vbg" : {
            //     n : "vbg",
            //     sr : 1,
            //     w : 320,
            //     h : 320
            // },
            // //核心
            // "vhe" : {
            //     n : "vhe",
            //     sr : 1,
            //     w : 230,
            //     h : 230
            // },
            // //dna
            // "vht" : {
            //     n : "vht",
            //     sr : 1,
            //     w : 108,
            //     h : 145
            // },
            // //血量
            // "vrt" : {
            //     n : "vrt",
            //     sr : 1,
            //     w : 260,
            //     h : 8,
            //     y : 166
            // },
            // //动画node
            // "van" : {
            //     n : "anim",
            //     sr : 1
            // }
        };
    },
    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        this._super();
        this._defBaseInfo = {
            refer : $G.get("viruspanel" , 'node'),
            toW : 1 , 
            toH : 1 ,
            stW : .5,
            stH : .5,
            stO : 160
        }
    },

    start () {
        this._super();
    },

    run( pos ){
        this._super(pos);
    },

    reset(){
        this._super();
        return this;
    },

    setBaseInfo( p ){
        let base = p && p.base ? p.base : this._defBaseInfo,
            vinf = p && p.vinf ? p.vinf : null;
        this._vinf = vinf || this._vinf;
        this._setSize( this._vinf );

        this.childNode("vbg").setPosition(cc.v2(0,0));

        this.init25D( base );
        return this;
    },

    _setSize( p ){
        p = p || this._vinf;
        let sr = p.sr || 1;
        let nodesNames = ['vbg' , 'vhe' , 'vht'];
        for(let k in p){
            if(k=="sr" ) continue;
            let nsr = p[k].sr || 1 ;
            let node = ( k=="node" ? this.node : this.childNode( p[k].n ));
            if(!node) continue;
            nsr = nsr * sr;

            let size = (p[k].w || p[k].h) ? [p[k].w?p[k].w*nsr:null , p[k].h?p[k].h*nsr:null] : null;

            if( p[k].sprite ){
                $RES.use( $CONF.do("virus" , "transRes" , p[k].sprite) )
                .then(( o )=>{
                    $T.setSprite( node , o , size);
                    //node && size && $T.setNodeSize( node , size );
                })
            }else{
                $T.setSprite( node , null );
                node && size && $T.setNodeSize( node , size );
            }

            nodesNames.indexOf(k)>=0 ? nodesNames.splice(nodesNames.indexOf(k),1) : null;
        }
        for(let i in nodesNames){
            $T.setSprite( this.childNode( nodesNames[i]) , null )
        }
    },

    //金币
    _playJingbi( bv , pos){
        const frame = $RES.g("imgs-jinbi-frm");
        const frames = [
            frame.getSpriteFrame("yi"),
            frame.getSpriteFrame("er"),
            frame.getSpriteFrame("san"),
            frame.getSpriteFrame("si")
        ];
        let parent = $G.get("viruspanel" , "node");
        
        let rotations = ((num)=>{
            //[0 , 30 , 60 , 90 , 120 , 150, 180 , 210 , 240 , 270 , 300 , 330 ];
            let tm = [] , re=[];
            for(let i=0; i<num; i++){
                let ti = $T.random(1,12)-1;
                while(tm.indexOf(ti)>=0){
                    ti = $T.random(1,12)-1;
                }
                tm.push(ti);
                re.push( ti*30 );
            }
            return re;
        })(3)

        let dofn = ( i )=>{
            if( $T.isType(rotations[i] , "Undefined") ) return;

            let node = $G.get('poolGetByName' , 'jingbi') || $RES.createPrefab("fab-anode");
            node.parent = parent;
            node.zIndex = 100;
            node.setPosition( parent.convertToNodeSpaceAR(parent.convertToWorldSpaceAR( pos )) );

            let topos = $T.getAnglePos( parent.convertToWorldSpaceAR( pos ) , rotations[i] , 100 );
            topos = parent.convertToNodeSpaceAR(topos);
            $T.getScr(node).reuse().playFreameAnimate([
                {
                    frame : frames[0],
                    size : null,
                    scale : 1, 
                    time : .1,
                    param : {},
                    //rotation: [0 , $T.random(100,180) ],
                    callfn : ()=>{
                    }
                },
                {
                    frame : frames[1],
                    size : null,
                    scale : 1, 
                    time : .1,
                    param : {},
                    //rotation: [0 , $T.random(100,180) ],
                    callfn : ()=>{
                    }
                },
                {
                    frame : frames[2],
                    size : null,
                    scale : 1, 
                    time : .1,
                    param : {},
                    //rotation: [0 , $T.random(100,180) ],
                    callfn : ()=>{
                    }
                },
                {
                    frame : frames[3],
                    size : null,
                    scale : 1, 
                    time : .1,
                    param : {},
                    //rotation: [0 , $T.random(100,180) ],
                    callfn : ()=>{
                    }
                }
            ], true);
    
            node.__myaction =  cc.tween( node )
            .to(.1 , { x:topos.x , y:topos.y})
            .delay(.3 - .03*i)
            .call(()=>{
                $Animate.do("flyNodeToNodeBezier" , 
                            node , 
                            $T.findChildNode("coins" , $G.get("menuspanel","node")),
                            {
                                height : 100,
                                angle : 90
                            },
                            .8
                )
                .then(()=>{
                    node.__myaction && node.__myaction.stop();
                    node.__myaction = null;
                    $T.getScr(node).unuse();
                    $G.do('poolPutByNode' , "put" , node , 'jingbi');
                })
            });
            node.__myaction.start();

            dofn(i+1);
        }

        dofn(0);

    },

    playBoom(){
        const frames = [
            $RES.g("imgs-boom-blue"),
            $RES.g("imgs-boom-green"),
            $RES.g("imgs-boom-green1"),
            $RES.g("imgs-boom-yellow"),
            $RES.g("imgs-boom-zise"),
            $RES.g("imgs-boom-yellow1")
        ]
        let nodes = [] , parent = this.node.parent , pos = this.node.getPosition(),
            size = [this.node.width>280?280:this.node.width,];
        for(let i=0; i<1; i++){
            nodes.push( $G.get('poolGetByName' , 'anode') || $RES.createPrefab("fab-anode") );
        }
        const dofn = ( i )=>{
            let node = nodes[i];
            if(!node) {
                this._playJingbi( 50 , pos);
                return;
            };
            const frame = frames[$T.random(1,frames.length)-1]
            node.parent = parent;
            node.zIndex = 50;
            node.setPosition( parent.convertToNodeSpaceAR(parent.convertToWorldSpaceAR( pos )) );
    //img-vboom-boom1
            $T.getScr(node).playFreameAnimate([
                {
                    frame : $RES.g($T.random(1,10)>5 ? "img-vboom-boom1" : "img-vboom-boom2"),
                    size : [size[0]*2 ,],
                    scale : 1, 
                    time : .06,
                    param : {}
                },
                {
                    frame : frame.getSpriteFrame("f1"),
                    size : [size[0]*1.2 ,],
                    scale : 1, 
                    time : .06,
                    param : {},
                    //rotation: [0 , $T.random(100,180) ],
                    callfn : ()=>{
                    }
                },
                {
                    frame :  frame.getSpriteFrame("f2"),//$RES.g("img-tx-vbooma"),
                    size : [size[0]*1.5 ,],
                    scale : 1, 
                    time : .1,
                    param : {},
                    //rotation: [0 , $T.random(100,180) ],
                    callfn : ()=>{
                    }
                },
                {
                    frame :  frame.getSpriteFrame("f3"),//$RES.g("img-tx-vbooma"),
                    size : [size[0]*1.6 ,],
                    scale : 1, 
                    time : .06,
                    param : {},
                    //rotation: [0 , $T.random(100,180) ],
                    callfn : ()=>{
                    }
                },

                $T.random(1,10)>5 ? {
                    frame :  frame.getSpriteFrame("f4"),//$RES.g("img-tx-vbooma"),
                    size : [size[0]*1.8 ,],
                    scale : 1,//[ 0.5, 3] , 
                    time : .1,
                    param : {},
                    //rotation: [0 , $T.random(100,180) ],
                    callfn : ()=>{
                    }
                } : 
                {
                    frame :  $RES.g("img-vboom-boomB1"),//$RES.g("img-tx-vbooma"),
                    size : [size[0]*1.8 ,],
                    scale : 1,//[ 0.5, 3] , 
                    time : .06,
                    param : {},
                    //rotation: [0 , $T.random(100,180) ],
                    callfn : ()=>{
                    }
                }
            ]).then(()=>{
                $G.do('poolPutByNode' , "put" , node , "anode");

            });

            setTimeout(() => {
                dofn(i+1);
            }, 100);
        }

        dofn(0);
    },

    //分裂
    divide( num , pos ){
        num = num || 1;
        let pool = $G.get("pool-virus");
        const viruspanel = $G.get("viruspanel");

        viruspanel.loadVirus( num ,  pos );
        
    },

    boom( killv ){
        let Dm = killv || 20;
        let hp = this.getAttr("HP");
        let isBooming = this.getAttr("_booming");
        const pos = this.node.getPosition();
        $Animate.do("playShake" , this.childNode("vhe") , 3);

        if(isBooming) return;

        if(hp-Dm <= 0){
            this.setAttr("_booming" , true );
            const pool = $G.get("pool-virus");
            const annode = this.childNode( this._vinf.van.n );
            const anim = annode.getComponent(cc.Animation);

            this.setAttr("updateOnOff" , false );
            annode.active = true;
            $Animate.do("playPop" , annode);
            $RES.playVoice("sound-boom");
            this.playBoom();

            this.playAnimates(false);
            cc.tween(this.node)
            .to(0.05 , {opacity:60})
            .start();
            //console.log( pos );
            $T.random(1,10)<4 ? this.divide(2 , pos) : null;
            
            //pool.put(this.node);
            // setTimeout(() => {
            //     // this.setAttr("_booming" , false );
            //     // $G.get("viruspanel").removeVirus( this.getAttr("_vid") );
            //     // this.setPosition25D(0,0,0);
            //     // this.reset();
            //     // pool.put( this.node );
            // },100);
            setTimeout(() => {
                this.unuse();
            }, 300);

        }else{
            this.setAttr("HP => sub:"+Dm);
        }
    },

    playAnimates( flag ){
        flag = $T.selectValue(flag , true);
        if(this.getAttr("updateOnOff") && flag){
            this._vinf.vhe && (
            this._animates.he = 
            cc.tween( this.childNode( this._vinf.vhe.n ))
            .to(0 , {rotation:0})
            .to( $T.random(8,12), {rotation: $T.randomsign() * 360})
            .repeatForever()
            .start())

            this._vinf.vbg && (
            this._animates.vbg = 
            cc.tween( this.childNode( this._vinf.vbg.n ))
            .to(0 , {rotation:0})
            .to( $T.random(2,4), {rotation: $T.randomsign() * 360})
            .repeatForever()
            .start());

        }else if(!flag){
            this._animates.he && this._animates.he.stop();
            this._animates.vbg && this._animates.vbg.stop();
            this._animates.he = null;
            this._animates.vbg = null;
        }
    },

    onPosition( possize , lvw , lvh ){
        let cur = { w:this.node.width,h:this.node.height },
            to  = possize.size;
        let bvw = lvw || (to.w/cur.w) , bvh = lvh || (to.h/cur.h) ;

        let p = this._vinf;
        
        for(let k in p){
            if(k=="sr" || k=="van") continue;
            let node = this.childNode( p[k].n );
            let last;
            if(!node){
                continue;
            }
            node && $T.setNodeSize( node , [ node.width * bvw , node.height * bvw ]);
            // if(k=="vbg" && !node.___vim ){
            //     console.log( "%d,%d | %d,%d" , last.w , last.h , node.width , node.height );
            //     node.___vim = true;
            // }else if(k=="vbg" &&  node.___vim ){
            //     console.log("%d,%d | %d,%d | %d,%d" , last.w , last.h , node.width , node.height , possize.size.w , possize.size.h );
            // }
        }
    }
    
});
 